package com.pingancar.creditmanage.action;

import com.pingancar.creditmanage.action.auth.ShopAuthPassport;
import com.pingancar.creditmanage.pojo.OrderitemPojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.pojo.ShopuserPojo;
import com.pingancar.creditmanage.service.ShopServiceImpl;
import com.pingancar.creditmanage.util.InitListener;
import com.pingancar.creditmanage.util.Signature;
import com.pingancar.creditmanage.util.myenum.ShopInfoField;
import com.pingancar.creditmanage.util.myenum.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by kx on 2014/9/13
 */
@Controller
@RequestMapping(value="/shop")
@ShopAuthPassport
public class ShopAction {
    @Autowired
    private ShopServiceImpl shopService;

    public static final String DEFAULT_ACTION = "/shop/loginPage";

    @ShopAuthPassport(validate = false)
    @RequestMapping("/loginPage")
    public String loginPage() {
        return "shop/login";
    }

    @RequestMapping("/")
    public String defaultPage(HttpSession session, ModelMap map) {
        Signature s = InitListener.getSignature("http://wechat.meiye.tv/pa/shop/");
        if (s == null)
            return "shop/error";
        map.addAttribute("s", s);
        return "shop/main";
    }

    @ShopAuthPassport(validate = false)
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        ShopuserPojo shopUserPojo = new ShopuserPojo();
        shopUserPojo.setUsername(username);
        shopUserPojo.setPasswd(password);

        shopUserPojo = shopService.login(shopUserPojo);
        if (shopUserPojo != null){
            session.setAttribute("shop", shopUserPojo.getShopinfo());
            session.setAttribute("shopuser", shopUserPojo);
            session.setAttribute("type", UserType.Producer);

            return "redirect:/shop/";
        }else {
            //addActionError("商家登陆失败");
            return "redirect:/shop/loginPage";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("type", UserType.Reader);
        session.removeAttribute("user");
        return "shop/login";
    }

    @RequestMapping("/infoPage")
    public String infoPage(HttpSession session, ModelMap modelMap) {
        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");
        modelMap.addAttribute("shop", shopinfoPojo);
        return "shop/modifyinfo";
    }

    @RequestMapping("/pwPage")
    public String pwPage() {
        return "shop/modifypw";
    }

    @RequestMapping("/userPage")
    public String userPage() {
        return "shop/adduser";
    }

    @RequestMapping("/listPage")
    public String listPage(HttpSession session, ModelMap modelMap) {
        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");

        modelMap.addAttribute("shopid", shopinfoPojo.getId());
        return "shop/orderlist";
    }

    @RequestMapping("/confirmPage")
    public String confirmPage(String id, HttpSession session, ModelMap modelMap) {
        if (id == null)
            return "shop/error";

        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");
        OrderitemPojo orderitemPojo = shopService.getOrderByOrderid(id, shopinfoPojo);
        if (orderitemPojo == null || orderitemPojo.getStatus() != OrderitemPojo.ORDERING)
            return "shop/error";
        modelMap.addAttribute("order", orderitemPojo);
        return "shop/confirm";
    }

    @RequestMapping("/addShopInfo")
    public String addShopInfo(ShopinfoPojo shopInfoPojo){
        boolean result = shopService.addShopInfo(shopInfoPojo);
        if (result){
            return "/";
        }else {
            //addActionError("添加商家信息失败");
            return "/";
        }
    }

    @RequestMapping("/updateShopInfo")
    public String updateShopInfo(ShopinfoPojo shopInfoPojo, HttpSession session){
        shopInfoPojo = shopService.updateShopInfo(shopInfoPojo);
        if (shopInfoPojo != null){
            session.setAttribute("shop", shopInfoPojo);
            return "shop/main";
        }else {
            //addActionError("更新商家信息失败");
            return "shop/error";
        }
    }

    @RequestMapping("/updatePw")
    public String updatePassword(String oldpw, String newpw, String repw, HttpSession session) {
        if (newpw == null || repw == null || !newpw.equals(repw))
            return "shop/error";
        ShopuserPojo shopuserPojo = (ShopuserPojo)session.getAttribute("shopuser");

        shopuserPojo = shopService.updatePassword(shopuserPojo, oldpw, newpw);
        if (shopuserPojo != null) {
            session.setAttribute("shopuser", shopuserPojo);
            return "shop/main";
        } else {
            return "shop/error";
        }
    }

    @RequestMapping("/deleteShopInfo")
    public String deleteShopInfo(ShopinfoPojo shopInfoPojo){
        boolean result = shopService.deleteShopInfo(shopInfoPojo);
        if (result){
            return "/";
        }else {
            //addActionError("删除商家信息失败");
            return "/";
        }
    }

    @RequestMapping("/addShopUser")
    public String addShopUser(String username, HttpSession session){
        if (username == null)
            return "shop/error";

        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");

        boolean result = shopService.addShopUser(shopinfoPojo, username);
        if (result){
            return "shop/main";
        }else {
            //addActionError("添加商家用户失败");
            return "shop/error";
        }
    }

    @RequestMapping("/updateShopUser")
    public String updateShopUser(ShopuserPojo shopUserPojo){
        boolean result = shopService.updateShopUser(shopUserPojo);
        if (result){
            return "/";
        }else {
            //addActionError("更新商家用户失败");
            return "/";
        }
    }

    @RequestMapping("/deleteShopUser")
    public String deleteShopUser(ShopuserPojo shopUserPojo){
        boolean result = shopService.deleteShopUser(shopUserPojo);
        if (result){
            return "/";
        }else {
            //addActionError("删除商家用户失败");
            return "/";
        }
    }

    @RequestMapping("/confirmOrder")
    public String confirmOrder(Integer id, HttpSession session) {
        if (id == null)
            return "shop/error";

        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");
        shopinfoPojo = shopService.confirmOrder(shopinfoPojo, id);
        if (shopinfoPojo == null)
            return "shop/error";

        session.setAttribute("shop", shopinfoPojo);
        return "redirect:./listPage";
    }

    @RequestMapping("/cash")
    public String cash(HttpSession session) {
//        ShopinfoPojo shopinfoPojo = (ShopinfoPojo)session.getAttribute("shop");
//        shopinfoPojo = shopService.cash(shopinfoPojo);
//
//        if (shopinfoPojo == null)
//            return "shop/error";
//
//        session.setAttribute("shop", shopinfoPojo);

        return "shop/main";
    }
}
